﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;

namespace UtilZ.Dotnet.Ex
{
    /// <summary>
    /// 枚举类型转换器
    /// </summary>
    /// <typeparam name="T">枚举类型</typeparam>
    /// <typeparam name="TV">枚举值类型(byte-int16-int32等)</typeparam>
    public class EnumParser<T, TV> : IEnumParser
        where T : Enum
        where TV : struct
    {
        /// <summary>
        /// 枚举类型名称
        /// </summary>
        private readonly string _enumTypeName;

        /// <summary>
        /// 枚举数值类型
        /// </summary>
        private readonly PrimitiveDataTypes _enumNumberType;

        /// <summary>
        /// 枚举字符串与枚举值字典集合[key:枚举值对应的字符串;value:枚举值]
        /// </summary>
        private readonly IReadOnlyDictionary<string, T> _enumStrEnumValueDic;
        /// <summary>
        /// 枚举字符串与枚举值字典集合[key:枚举值对应的字符串;value:枚举值]
        /// </summary>
        public IReadOnlyDictionary<string, T> EnumStrEnumValueDic
        {
            get { return _enumStrEnumValueDic; }
        }

        /// <summary>
        /// 枚举数值与枚举值字典集合[key:枚举数据值;value:枚举值]
        /// </summary>
        private readonly IReadOnlyDictionary<TV, T> _numberEnumValueDic;
        /// <summary>
        /// 枚举数值与枚举值字典集合[key:枚举数据值;value:枚举值]
        /// </summary>
        public IReadOnlyDictionary<TV, T> NumberEnumValueDic
        {
            get { return _numberEnumValueDic; }
        }

        /// <summary>
        /// 枚举值与枚举字符串字典集合[key:枚举值;value:枚举值对应的字符串]
        /// </summary>
        private readonly IReadOnlyDictionary<T, string> _enumValueEnumStrDic;
        /// <summary>
        /// 枚举值与枚举字符串字典集合[key:枚举值;value:枚举值对应的字符串]
        /// </summary>
        public IReadOnlyDictionary<T, string> EnumValueEnumStrDic
        {
            get { return _enumValueEnumStrDic; }
        }



        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="enumTypeName">枚举类型名称</param>
        /// <param name="enumStrEnumValueDic">枚举字符串与枚举值字典集合[key:枚举值对应的字符串;value:枚举值]</param>
        /// <param name="enumNumberType">枚举数值类型</param>
        public EnumParser(string enumTypeName, IReadOnlyDictionary<string, T> enumStrEnumValueDic, PrimitiveDataTypes enumNumberType)
        {
            this._enumTypeName = enumTypeName;
            this._enumStrEnumValueDic = enumStrEnumValueDic;
            this._enumNumberType = enumNumberType;

            var numberEnumValueDic = new Dictionary<TV, T>();
            var enumValueEnumStrDic = new Dictionary<T, string>();

            foreach (var kv in enumStrEnumValueDic)
            {
                numberEnumValueDic.Add((TV)(((object)kv.Value)), kv.Value);
                enumValueEnumStrDic.Add(kv.Value, kv.Key);
            }

            this._numberEnumValueDic = numberEnumValueDic;
            this._enumValueEnumStrDic = enumValueEnumStrDic;
        }



        /// <summary>
        /// 解析枚举字符串转换为枚举值
        /// </summary>
        /// <param name="value">枚举字符串</param>
        /// <param name="ignoreCase">字符串转换时是否忽略大小写[true:忽略大小写;false:区分大小写]</param>
        /// <returns>枚举值</returns>
        /// <exception cref="ArgumentNullException">参数这空异常</exception>
        /// <exception cref="InvalidCastException">转换不匹配异常</exception>
        public T Parse(string value, bool ignoreCase = true)
        {
            if (string.IsNullOrWhiteSpace(value))
            {
                throw new ArgumentNullException(nameof(value), "枚举值字符串不能为null或空或全空格");
            }

            if (this._enumStrEnumValueDic.TryGetValue(value, out var result))
            {
                return result;
            }

            if (ignoreCase)
            {
                foreach (var kv in this._enumStrEnumValueDic)
                {
                    if (string.Equals(kv.Key, value, StringComparison.OrdinalIgnoreCase))
                    {
                        return kv.Value;
                    }
                }
            }

            throw new InvalidCastException($"字符串值\"{value}\"转换成目标枚举类型\"{this._enumTypeName}\"失败");
        }

        /// <summary>
        /// 解析枚举字符串转换为枚举值
        /// </summary>
        /// <param name="value">枚举字符串</param>
        /// <param name="ignoreCase">字符串转换时是否忽略大小写[true:忽略大小写;false:区分大小写]</param>
        /// <returns>枚举值</returns>
        public T Parse(ReadOnlySpan<char> value, bool ignoreCase = true)
        {
            string valueStr = value.ToString();
            return this.Parse(valueStr, ignoreCase);
        }

        /// <summary>
        /// 解析枚举值转换为枚举值
        /// </summary>
        /// <param name="value">枚举数值</param>
        /// <returns>枚举值</returns>
        /// <exception cref="InvalidCastException">转换不匹配异常</exception>
        public T Parse(TV value)
        {
            if (this._numberEnumValueDic.TryGetValue(value, out var result))
            {
                return result;
            }

            throw new InvalidCastException($"整数值\"{value}\"转换成目标枚举类型\"{this._enumTypeName}\"失败");
        }




        /// <summary>
        /// 尝试解析枚举字符串转换为枚举值,转换成功返回true,失败返回false
        /// </summary>
        /// <param name="value">枚举字符串</param>
        /// <param name="result">枚举值</param>
        /// <returns>转换成功返回true,失败返回false</returns>
        public bool TryParse(string value, out T result)
        {
            if (string.IsNullOrWhiteSpace(value))
            {
                result = default(T);
                return false;
            }

            if (this._enumStrEnumValueDic.TryGetValue(value, out result))
            {
                return true;
            }

            return false;
        }

        /// <summary>
        /// 尝试解析枚举字符串转换为枚举值,转换成功返回true,失败返回false
        /// </summary>
        /// <param name="value">枚举字符串</param>
        /// <param name="ignoreCase">字符串转换时是否忽略大小写[true:忽略大小写;false:区分大小写]</param>
        /// <param name="result">枚举值</param>
        /// <returns>转换成功返回true,失败返回false</returns>
        public bool TryParse(string value, bool ignoreCase, out T result)
        {
            if (string.IsNullOrWhiteSpace(value))
            {
                result = default(T);
                return false;
            }

            if (this._enumStrEnumValueDic.TryGetValue(value, out result))
            {
                return true;
            }

            if (ignoreCase)
            {
                foreach (var kv in this._enumStrEnumValueDic)
                {
                    if (string.Equals(kv.Key, value, StringComparison.OrdinalIgnoreCase))
                    {
                        result = kv.Value;
                        return true;
                    }
                }
            }

            return false;
        }

        /// <summary>
        /// 尝试解析枚举字符串转换为枚举值,转换成功返回true,失败返回false
        /// </summary>
        /// <param name="value">枚举字符串</param>
        /// <param name="result">枚举值</param>
        /// <returns>转换成功返回true,失败返回false</returns>
        public bool TryParse(ReadOnlySpan<char> value, out T result)
        {
            string valueStr = value.ToString();
            return this.TryParse(valueStr, out result);
        }

        /// <summary>
        /// 尝试解析枚举字符串转换为枚举值,转换成功返回true,失败返回false
        /// </summary>
        /// <param name="value">枚举字符串</param>
        /// <param name="result">枚举值</param>
        /// <param name="ignoreCase">字符串转换时是否忽略大小写[true:忽略大小写;false:区分大小写]</param>
        /// <returns>转换成功返回true,失败返回false</returns>
        public bool TryParse(ReadOnlySpan<char> value, bool ignoreCase, out T result)
        {
            string valueStr = value.ToString();
            return this.TryParse(valueStr, ignoreCase, out result);
        }

        /// <summary>
        /// 尝试解析枚举字符串转换为枚举值,转换成功返回true,失败返回false
        /// </summary>
        /// <param name="value">枚举数值</param>
        /// <param name="result">枚举值</param>
        /// <param name="ignoreCase">字符串转换时是否忽略大小写[true:忽略大小写;false:区分大小写]</param>
        /// <returns>转换成功返回true,失败返回false</returns>
        public bool TryParse(TV value, out T result)
        {
            if (this._numberEnumValueDic.TryGetValue(value, out result))
            {
                return true;
            }

            return false;
        }



        /// <summary>
        /// 将枚举值转换烽string
        /// </summary>
        /// <param name="value">枚举值</param>
        /// <returns>枚举值字符串</returns>
        public string GetEnumString(T value)
        {
            if (this._enumValueEnumStrDic.TryGetValue(value, out var str))
            {
                return str;
            }
            else
            {
                return value.ToString();
            }
        }

        /// <summary>
        /// 获取枚举数值
        /// </summary>
        /// <param name="value">枚举值</param>
        /// <returns></returns>
        public TV GetEnumIntegerValue(T value)
        {
            foreach (var kv in this._numberEnumValueDic)
            {
                if (object.Equals(kv.Value, value))
                {
                    return kv.Key;
                }
            }

            throw new ArgumentException($"枚举类型\"{this._enumTypeName}\"中值\"{value}\"对应的枚举数值映射不存在");
        }


        #region IEnumParser接口
        /// <summary>
        /// 枚举类型名称
        /// </summary>
        public string EnumTypeName
        {
            get { return this._enumTypeName; }
        }

        /// <summary>
        /// 枚举数值类型
        /// </summary>
        public PrimitiveDataTypes EnumNumberType
        {
            get { return this._enumNumberType; }
        }

        /// <summary>
        /// 解析枚举字符串转换为枚举值
        /// </summary>
        /// <param name="value">枚举字符串</param>
        /// <param name="ignoreCase">字符串转换时是否忽略大小写[true:忽略大小写;false:区分大小写]</param>
        /// <returns>枚举值</returns>
        /// <exception cref="ArgumentNullException">参数这空异常</exception>
        /// <exception cref="InvalidCastException">转换不匹配异常</exception>
        object IEnumParser.Parse(string value, bool ignoreCase)
        {
            return this.Parse(value, ignoreCase);
        }

        /// <summary>
        /// 解析枚举字符串转换为枚举值
        /// </summary>
        /// <param name="value">枚举字符串</param>
        /// <param name="ignoreCase">字符串转换时是否忽略大小写[true:忽略大小写;false:区分大小写]</param>
        /// <returns>枚举值</returns>
        object IEnumParser.Parse(ReadOnlySpan<char> value, bool ignoreCase)
        {
            return this.Parse(value, ignoreCase);
        }

        /// <summary>
        /// 解析枚举值转换为枚举值
        /// </summary>
        /// <param name="value">枚举数值</param>
        /// <returns>枚举值</returns>
        /// <exception cref="InvalidCastException">转换不匹配异常</exception>
        object IEnumParser.Parse(object integerValue)
        {
            return this.Parse((TV)integerValue);
        }



        /// <summary>
        /// 尝试解析枚举字符串转换为枚举值,转换成功返回true,失败返回false
        /// </summary>
        /// <param name="value">枚举字符串</param>
        /// <param name="ignoreCase">字符串转换时是否忽略大小写[true:忽略大小写;false:区分大小写]</param>
        /// <param name="result">枚举值</param>
        /// <returns>转换成功返回true,失败返回false</returns>
        bool IEnumParser.TryParse(string value, bool ignoreCase, out object result)
        {
            T enumValue;
            bool ret = this.TryParse(value, ignoreCase, out enumValue);
            if (ret)
            {
                result = enumValue;
            }
            else
            {
                result = null;
            }
            return ret;
        }

        /// <summary>
        /// 尝试解析枚举字符串转换为枚举值,转换成功返回true,失败返回false
        /// </summary>
        /// <param name="value">枚举字符串</param>
        /// <param name="ignoreCase">字符串转换时是否忽略大小写[true:忽略大小写;false:区分大小写]</param>
        /// <param name="result">枚举值</param>
        /// <returns>转换成功返回true,失败返回false</returns>
        bool IEnumParser.TryParse(ReadOnlySpan<char> value, bool ignoreCase, out object result)
        {
            T enumValue;
            bool ret = this.TryParse(value, ignoreCase, out enumValue);
            if (ret)
            {
                result = enumValue;
            }
            else
            {
                result = null;
            }
            return ret;
        }

        /// <summary>
        /// 尝试解析枚举字符串转换为枚举值,转换成功返回true,失败返回false
        /// </summary>
        /// <param name="value">枚举数值</param>
        /// <param name="result">枚举值</param>
        /// <param name="ignoreCase">字符串转换时是否忽略大小写[true:忽略大小写;false:区分大小写]</param>
        /// <returns>转换成功返回true,失败返回false</returns>
        bool IEnumParser.TryParse(object value, out object result)
        {
            T enumValue;
            bool ret = this.TryParse((TV)value, out enumValue);
            if (ret)
            {
                result = enumValue;
            }
            else
            {
                result = null;
            }
            return ret;
        }

        /// <summary>
        /// 获取枚举数值
        /// </summary>
        /// <param name="value">枚举值</param>
        /// <returns></returns>
        object IEnumParser.GetEnumIntegerValue(object value)
        {
            return this.GetEnumIntegerValue((T)value);
        }
        #endregion

    }


    /// <summary>
    /// 枚举类型转换器接口
    /// </summary>
    public interface IEnumParser
    {

        /// <summary>
        /// 枚举类型名称
        /// </summary>
        string EnumTypeName { get; }

        /// <summary>
        /// 枚举数值类型
        /// </summary>
        PrimitiveDataTypes EnumNumberType { get; }

        /// <summary>
        /// 解析枚举字符串转换为枚举值
        /// </summary>
        /// <param name="value">枚举字符串</param>
        /// <param name="ignoreCase">字符串转换时是否忽略大小写[true:忽略大小写;false:区分大小写]</param>
        /// <returns>枚举值</returns>
        /// <exception cref="ArgumentNullException">参数这空异常</exception>
        /// <exception cref="InvalidCastException">转换不匹配异常</exception>
        object Parse(string value, bool ignoreCase = true);

        /// <summary>
        /// 解析枚举字符串转换为枚举值
        /// </summary>
        /// <param name="value">枚举字符串</param>
        /// <param name="ignoreCase">字符串转换时是否忽略大小写[true:忽略大小写;false:区分大小写]</param>
        /// <returns>枚举值</returns>
        object Parse(ReadOnlySpan<char> value, bool ignoreCase = true);

        /// <summary>
        /// 解析枚举值转换为枚举值
        /// </summary>
        /// <param name="value">枚举数值</param>
        /// <returns>枚举值</returns>
        /// <exception cref="InvalidCastException">转换不匹配异常</exception>
        object Parse(object integerValue);



        /// <summary>
        /// 尝试解析枚举字符串转换为枚举值,转换成功返回true,失败返回false
        /// </summary>
        /// <param name="value">枚举字符串</param>
        /// <param name="ignoreCase">字符串转换时是否忽略大小写[true:忽略大小写;false:区分大小写]</param>
        /// <param name="result">枚举值</param>
        /// <returns>转换成功返回true,失败返回false</returns>
        bool TryParse(string value, bool ignoreCase, out object result);

        /// <summary>
        /// 尝试解析枚举字符串转换为枚举值,转换成功返回true,失败返回false
        /// </summary>
        /// <param name="value">枚举字符串</param>
        /// <param name="ignoreCase">字符串转换时是否忽略大小写[true:忽略大小写;false:区分大小写]</param>
        /// <param name="result">枚举值</param>
        /// <returns>转换成功返回true,失败返回false</returns>
        bool TryParse(ReadOnlySpan<char> value, bool ignoreCase, out object result);

        /// <summary>
        /// 尝试解析枚举字符串转换为枚举值,转换成功返回true,失败返回false
        /// </summary>
        /// <param name="value">枚举数值</param>
        /// <param name="result">枚举值</param>
        /// <param name="ignoreCase">字符串转换时是否忽略大小写[true:忽略大小写;false:区分大小写]</param>
        /// <returns>转换成功返回true,失败返回false</returns>
        bool TryParse(object value, out object result);

        /// <summary>
        /// 获取枚举数值
        /// </summary>
        /// <param name="value">枚举值</param>
        /// <returns></returns>
        object GetEnumIntegerValue(object value);
    }
}
